﻿using ST.Library.UI.NodeEditor;
using STNode_A2B.DataType;
using STNode_A2B.STNodeControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STNode_A2B.STNodes
{
    [STNode("A2B")]
    public class STNode_Sink : STNode
    {
        private STNodeControl_CheckBox m_ctrl_IIC_checkbox;
        private STNodeControl_CheckBox m_ctrl_Rx0_checkbox;
        private STNodeControl_CheckBox m_ctrl_Rx1_checkbox;

        private STNodeControl_TextBox m_ctrl1_IIC_TextBox;
        private STNodeControl_TextBox m_ctrl1_Rx0_TextBox;
        private STNodeControl_TextBox m_ctrl1_RX1_TextBox;

        private STNodeOption m_op_in_I2C;
        private STNodeOption m_op_in_Rx0;
        private STNodeOption m_op_in_Rx1;

        private STNodeOption m_op_in;

        private bool _isCheckedIIC;
        [STNodeProperty("IIC", "This is IIC checkbox")]
        public bool IsCheckedIIC
        {
            get { return _isCheckedIIC; }
            set { _isCheckedIIC = value; m_ctrl_IIC_checkbox.Checked = value; }
        }

        private bool _isCheckedRx0;
        [STNodeProperty("Rx0", "This is Tx0 checkbox")]
        public bool IsCheckedRx0
        {
            get { return _isCheckedRx0; }
            set { _isCheckedRx0 = value; m_ctrl_Rx0_checkbox.Checked = value; }
        }

        private bool _isCheckedRx1;
        [STNodeProperty("Rx1", "This is Tx1 checkbox")]
        public bool IsCheckedRx1
        {
            get { return _isCheckedRx1; }
            set { _isCheckedRx1 = value; m_ctrl_Rx1_checkbox.Checked = value; }
        }

        private int _IIC_Addr = 44;
        [STNodeProperty("IIC Address", "IIC Address")]
        public string Addr
        {
            get { return _IIC_Addr.ToString(); }
            set
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    m_ctrl1_IIC_TextBox.EditorText = value;
                    _IIC_Addr = int.Parse(value);
                }
                Invalidate();
            }
        }

        private int _Rx0_Data;
        [STNodeProperty("Rx0", "Rx0 Data")]
        public string Rx0_Data
        {
            get { return _Rx0_Data.ToString(); }
            set
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    m_ctrl1_Rx0_TextBox.EditorText = value;
                    _Rx0_Data = int.Parse(value);
                }
                Invalidate();
            }
        }

        private int _Rx1_Data;
        [STNodeProperty("Rx1", "Rx1 Data")]
        public string Rx1_Data
        {
            get { return _Rx1_Data.ToString(); }
            set
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    m_ctrl1_RX1_TextBox.EditorText = value;
                    _Rx1_Data = int.Parse(value);
                }
                Invalidate();
            }
        }

        private string _nodeName = "Sink";
        [STNodeProperty("Title", "Title")]
        public string NodeName
        {
            get { return _nodeName; }
            set
            {
                this._nodeName = value;
                this.Title = this._nodeName;
            }
        }
        protected override void OnCreate()
        {
            base.OnCreate();
            this.TitleColor = Color.FromArgb(200, Color.DarkKhaki);
            this.Title = "Sink";
            this.AutoSize = false;
            this.Size = new Size(190, 90);

            m_op_in_I2C = this.InputOptions.Add("", typeof(NoneType), true);
            m_op_in_Rx0 = this.InputOptions.Add("", typeof(NoneType), true);
            m_op_in_Rx1 = this.InputOptions.Add("", typeof(NoneType), true);

            m_ctrl_IIC_checkbox = new STNodeControl_CheckBox();
            m_ctrl_IIC_checkbox.Text = "I2C Adr";
            m_ctrl_IIC_checkbox.DisplayRectangle = new Rectangle(25, 0, 120, 20);
            m_ctrl_IIC_checkbox.ValueChanged += (s, e) =>
            {
                this._isCheckedIIC = m_ctrl_IIC_checkbox.Checked;
                if (!this._isCheckedIIC)
                {
                    m_op_in_I2C.DisConnectionAll();
                    m_op_in_I2C.DataType = typeof(NoneType);
                    m_op_in_I2C.DotColor = Color.Gray;
                }
                else
                {
                    m_op_in_I2C.DataType = typeof(IIC);
                    m_op_in_I2C.DotColor = Color.DarkCyan;
                }
            };
            this.Controls.Add(m_ctrl_IIC_checkbox);

            m_ctrl1_IIC_TextBox = new STNodeControl_TextBox();
            m_ctrl1_IIC_TextBox.IsAddress = true;
            m_ctrl1_IIC_TextBox.DisplayRectangle = new Rectangle(125, 0, 60, 20);
            m_ctrl1_IIC_TextBox.EditorText = Addr;
            this.Controls.Add(m_ctrl1_IIC_TextBox);

            m_ctrl_Rx0_checkbox = new STNodeControl_CheckBox();
            m_ctrl_Rx0_checkbox.Text = "Rx0-ch";
            m_ctrl_Rx0_checkbox.DisplayRectangle = new Rectangle(25, 20, 120, 20);
            m_ctrl_Rx0_checkbox.ValueChanged += (s, e) =>
            {
                this._isCheckedRx0 = m_ctrl_Rx0_checkbox.Checked;
                if (!this._isCheckedRx0)
                {
                    m_op_in_Rx0.DisConnectionAll();
                    m_op_in_Rx0.DataType = typeof(NoneType);
                    m_op_in_Rx0.DotColor = Color.Gray;
                }
                else
                {
                    m_op_in_Rx0.DataType = typeof(Tx0Rx0);
                    m_op_in_Rx0.DotColor = Color.DarkBlue;
                }
            };
            this.Controls.Add(m_ctrl_Rx0_checkbox);

            m_ctrl1_Rx0_TextBox = new STNodeControl_TextBox();
            m_ctrl1_Rx0_TextBox.DisplayRectangle = new Rectangle(125, 20, 60, 20);
            m_ctrl1_Rx0_TextBox.EditorText = Rx0_Data;
            this.Controls.Add(m_ctrl1_Rx0_TextBox);

            m_ctrl_Rx1_checkbox = new STNodeControl_CheckBox();
            m_ctrl_Rx1_checkbox.Text = "Rx1-ch";
            m_ctrl_Rx1_checkbox.DisplayRectangle = new Rectangle(25, 40, 120, 20);
            m_ctrl_Rx1_checkbox.ValueChanged += (s, e) =>
            {
                this._isCheckedRx1 = m_ctrl_Rx1_checkbox.Checked;
                if (!this._isCheckedRx1)
                {
                    m_op_in_Rx1.DisConnectionAll();
                    m_op_in_Rx1.DataType = typeof(NoneType);
                    m_op_in_Rx1.DotColor = Color.Gray;
                }
                else
                {
                    m_op_in_Rx1.DataType = typeof(Tx1Rx1);
                    m_op_in_Rx1.DotColor = Color.SkyBlue;
                }
            };
            this.Controls.Add(m_ctrl_Rx1_checkbox);

            m_ctrl1_RX1_TextBox = new STNodeControl_TextBox();
            m_ctrl1_RX1_TextBox.DisplayRectangle = new Rectangle(125, 40, 60, 20);
            m_ctrl1_RX1_TextBox.EditorText = Rx1_Data;
            this.Controls.Add(m_ctrl1_RX1_TextBox);
        }

        protected override void OnOwnerChanged()
        {
            base.OnOwnerChanged();

            if (this.Owner != null)
            {
                //this.Owner.SetTypeColor(typeof(IIC), Color.DarkCyan);
                //this.Owner.SetTypeColor(typeof(TxRx0), Color.DarkBlue);
                //this.Owner.SetTypeColor(typeof(TxRx1), Color.SkyBlue);
            }
        }
    }
}
